package org.ofbiz.client;

import java.util.Locale;
import java.util.Map;

//import org.apache.commons.dbcp.BasicDataSource;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.framework.spring.GenericSpring;
import org.ofbiz.framework.spring.SpringContext;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ServiceUtil;

public class EchoServices {

    public static final String module = EchoServices.class.getName();
    public static final String resource = "SpringUiLabels";

    public static Map<String, Object> springEchoClient(DispatchContext ctx, Map<String, ? extends Object> context) {
	// Delegator delegator = ctx.getDelegator();
	Locale locale = (Locale) context.get("locale");

	String inValue = (String) context.get("inValue");
	String inoutValue = (String) context.get("inoutValue");

	try {
	    SpringContext springctx = GenericSpring.getSpringContext("default");
	    Echo echo = (Echo) springctx.getBean("echo");
	    // System.out.println(echo.echoString(inValue));
	    String outValue = "out=" + echo.echoString(inValue);

	    // BasicDataSource ss = (BasicDataSource) springctx.getBean("dataSource");
	    // +ss.getUrl()+ss.getUsername()

	    Map<String, Object> results = ServiceUtil.returnSuccess();
	    results.put("inoutValue", "inout=" + inoutValue);
	    results.put("outValue", outValue);
	    return results;
	} catch (Exception e) {
	    Debug.logError(e, e.getMessage(), module);
	    return ServiceUtil.returnError(UtilProperties.getMessage(resource, "springEchoClient.error",
		    new Object[] { e.getMessage() }, locale));
	}
    }

}
